import checkBoxItem from '../from/checkBoxItem';
import radioItem from './radioItem';
import util from './util'
import optionSide from './optionSide';

var fileType = {
    csv: 'text/csv',
    xls: 'application/vnd.ms-excel'
};
function getScrollbarWidth() {
    var scrollDiv = document.createElement("div");
    scrollDiv.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
    document.body.appendChild(scrollDiv);
    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    document.body.removeChild(scrollDiv);
    return scrollbarWidth;
};
function hasScrollbar(el) {
    return el[0].scrollHeight > (el[0].innerHeight || el[0].clientHeight);
};
var isNumber = function (obj) {
    return obj == +obj
};

var _Title = function (tableParent) {
    this.cols = tableParent.cols;
    this.tableParent = tableParent;
    this._class = tableParent._class;
    this._theme = tableParent._theme;
    this.html = $(`<table cellspacing="0" cellpadding="0" border="0"  class="${this._class.table} ${this._theme.table}"><thead class="${this._class.header} ${this._theme.header}"></thead></table>`);
    this.html.css({
        'width': '100%',
        'overflow': 'hidden',
        'display': 'block',
    });
    this.$el = $(`<tr class="${this._class.headerBox} ${this._theme.headerBox}"></tr>`).appendTo(this.html.find('thead'));
    this.units = [];
    this.unitsKey = {};
    this.parentHtmlWidth = 0;
};
_Title.prototype.render = function () {
    this.cols = this.tableParent.cols;
    this.units = [];
    this.unitsKey = {};
    this.$el.empty();
    for (let i = 0; i < this.cols.length; i++) {
        let item = this.cols[i];
        if (i == this.cols.length - 1) {
            item.isLast = true;
        };
        var titleUnit = new _TitleUnit(this.tableParent, this, item);
        this.unitsKey[item.key] = titleUnit;
        this.units.push(titleUnit);
        this.$el.append(titleUnit.render());
    }
    this.setWidth();
};
_Title.prototype.setWidth = function () {
    var hasScroll = hasScrollbar(this.tableParent.objs.body.$el);

    var parentHtmlWidth = this.tableParent.html.width();
    var parentHtmlMark = parentHtmlWidth;
    if (hasScroll) {
        var scrollWidth = getScrollbarWidth();
        parentHtmlWidth -= scrollWidth;
        parentHtmlMark -= scrollWidth;
        this.html.css('width', parentHtmlWidth);
        // this.tableParent.html.find('.scroll-mark').show();
        this.tableParent.html.find('.scroll-mark').css({
            width: (scrollWidth - 1) + 'px',
            borderWidth: '1px',
        });
    } else {
        // this.tableParent.html.find('.scroll-mark').show();
        this.tableParent.html.find('.scroll-mark').css({
            width: '1px',
            borderWidth: '1px 1px 1px 0',
        });
    };

    var unitLength = this.cols.length;
    var unitWidth = 0;
    var widthArray = [];
    for (let i = 0; i < this.cols.length; i++) {
        const element = this.cols[i];
        if (element.hasOwnProperty('width') && element.width) {
            unitLength -= 1;
            var styleWidth = 0;
            if (element.width.indexOf('%') != -1) {
                styleWidth = parentHtmlMark * parseInt(element.width) / 100;
            } else {
                styleWidth = parseInt(element.width);
            };
            widthArray.push(styleWidth);
            parentHtmlWidth -= styleWidth;
        } else {
            widthArray.push(null);
        };
    };
    if (parentHtmlWidth > 0) {
        unitWidth = parseInt(parentHtmlWidth / unitLength);
        if (unitWidth < this.tableParent.options.minWidth) {
            unitWidth = this.tableParent.options.minWidth;
        };
    };
    var allWidth = 0;
    for (let i = 0; i < widthArray.length; i++) {
        let item = widthArray[i];
        if (!this.units[i].moveWidth) {
            if (item) {
                allWidth += item;
                this.units[i].setWidth(item);
            } else {
                allWidth += unitWidth;
                this.units[i].setWidth(unitWidth);
            };
        };
    };
    var lastUnit = this.units[this.units.length - 1];
    if (this.tableParent.html.width() >= allWidth) {
        lastUnit.setWidth(lastUnit.styleWidth += parentHtmlMark - allWidth - 1);
    };
    this.tableParent.objs.body.renderWidth();
};
_Title.prototype.clearSortStyle = function () {
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        element.clearSort();
    };
};
_Title.prototype.getCheckBoxId = function () {
    var id = null;
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.options.type == 'checkBox') {
            id = element.id;
        };
    };
    return id;
};
_Title.prototype.getRadioId = function () {
    var id = null;
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.options.type == 'radio') {
            id = element.id;
        };
    };
    return id;
};
_Title.prototype.getTitleValues = function () {
    var datas = [];
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.options.type != 'checkBox' && element.options.type != 'radio' && element.options.type != 'buttons') {
            datas.push({
                key: element.options.key,
                name: element.options.name,
            });
        };
    };
    return datas;
};
_Title.prototype.clearCheckBox = function () {
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        element.checkBox.set(false);
    };

};

var _TitleUnit = function (tableParent, titleParent, options) {
    this.id = util.uuid();
    this.options = options;
    this.tableParent = tableParent;
    this.titleParent = titleParent;
    this._class = tableParent._class;
    this._theme = tableParent._theme;
    this.options.textAlign = this.options.textAlign || 'left';
    this.moveWidth = 0;
    this.styleWidth = 0;
    this.typeFormat = null;
    var TypeIndex = this.options.type.indexOf(':');
    if (TypeIndex != -1) {
        var key = this.options.type.substring(0, TypeIndex);
        var value = this.options.type.substring(TypeIndex + 1, this.options.type.length);
        this.typeFormat = {
            key: key,
            value: value,
        }
    };

    this.html = $(`<th></th>`);
    this.$el = $(`<div class="${this._class.unit} ${this._class.headerUnit} ${this._theme.headerUnit}">${this.options.name}</div>`).appendTo(this.html);

    this.$el.css({
        'text-align': this.options.titleAlign || this.tableParent.options.titleAlign,
    });


    if (this.options.drag !== false) {
        this.drag = $(`<div class="${this._class.headerDrag} ${this._theme.headerDrag}"></div>`).appendTo(this.$el);
        this.drag.mousedown((event) => {
            document.body.style.cursor = 'col-resize';
            var nowX = event.clientX;
            var _styleWidth = this.styleWidth;
            document.onmousemove = (event) => {
                var movePX = event.clientX - nowX;
                var nowStyle = _styleWidth + movePX;
                if (nowStyle < this.tableParent.options.minWidth) {
                    nowStyle = this.tableParent.options.minWidth
                };
                this.setWidth(nowStyle);
                this.tableParent.objs.body.renderWidth(this.options.key);
            };
            document.onmouseup = () => {
                document.body.style.cursor = '';
                document.onmousemove = null;
                document.onmouseup = null;
            };
        });
    };

    this.checkBox = new checkBoxItem({
        data: {
            name: ''
        },
        onChange: value => {
            this.tableParent.objs.body.setCheckBox(value);
        },
    });
    if (this.options.type == 'checkBox') {
        this.$el.css('min-width', '40px');
        this.$el.empty();
        this.$el.append(this.checkBox.html);
    } else if (this.options.type == 'radio') {
        this.$el.css('min-width', '40px');
        this.$el.empty();
    };
    this.sortBy = null;
    if (this.options.sort) {
        this.sortEle = $(`<div class="${this._theme.headerUnit}_sort">
            <div class="${this._theme.headerUnit}_sort_asc">▲</div>
            <div class="${this._theme.headerUnit}_sort_gra">▼</div>
        </div>`).appendTo(this.$el);

        this.$el.click(() => {
            if (this.sortBy === null) {
                this.sortBy = 'asc';
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').removeClass('active');
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').addClass('active');
            } else if (this.sortBy === 'asc') {
                this.sortBy = 'gra';
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').addClass('active');
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').removeClass('active');
            } else if (this.sortBy === 'gra') {
                this.sortBy = null;
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').removeClass('active');
                this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').removeClass('active');
            };
            this.tableParent.sortBy(this.options.key, this.sortBy);
        });


        // this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').click(() => {
        //     if (this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').hasClass('active')) {
        //         this.titleParent.clearSortStyle();
        //         this.tableParent.sortBy(this.options.key, null);
        //     } else {
        //         this.titleParent.clearSortStyle();
        //         this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').addClass('active');
        //         this.tableParent.sortBy(this.options.key, 'asc');

        //     };
        // });
        // this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').click(() => {
        //     if (this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').hasClass('active')) {
        //         this.titleParent.clearSortStyle();
        //         this.tableParent.sortBy(this.options.key, null);
        //     } else {
        //         this.tableParent.sortBy(this.options.key, 'gra');
        //         this.titleParent.clearSortStyle();
        //         this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').addClass('active');
        //     };
        // });
    };
};
_TitleUnit.prototype.clearSort = function () {
    if (this.sortEle) {
        this.sortBy = null;
        this.sortEle.find('.' + this._theme.headerUnit + '_sort_gra').removeClass('active');
        this.sortEle.find('.' + this._theme.headerUnit + '_sort_asc').removeClass('active');
    };
};
_TitleUnit.prototype.render = function () {
    return this.html;
};
_TitleUnit.prototype.setWidth = function (width) {
    this.styleWidth = width;
    this.$el.css('width', this.styleWidth + 'px');
};
_TitleUnit.prototype.checkBoxValue = function () {
    var body = this.tableParent.objs.body;
    var values = body.getCheckBoxValues(this.id);
    var allCheck = true;
    for (let i = 0; i < values.length; i++) {
        if (!values[i]) {
            allCheck = false;
            break;
        };
    };
    this.checkBox.set(allCheck);
};

var _Body = function (tableParent) {
    this.tableParent = tableParent;
    this.titleParent = tableParent.objs.title;
    this._class = tableParent._class;
    this._theme = tableParent._theme;
    this.html = $(`<table cellspacing="0" cellpadding="0" border="0" style="width:100%" class="${this._class.bodyBox} ${this._theme.bodyBox}"></table>`);
    this.$el = $(`<tbody class="${this._class.body} ${this._theme.body}"></tbody>`).appendTo(this.html);
    if (this.tableParent.options.height) {
        this.$el.css({
            height: this.tableParent.options.height || 'auto'
        });
    };
    this.childrens = [];
    this.$el.scroll(() => {//开始监听滚动条
        var left = this.$el.scrollLeft();
        this.titleParent.html.scrollLeft(left);
    });
};
_Body.prototype.setData = function (data) {
    this.childrens = [];
    this.$el.empty();
    for (let i = 0; i < data.length; i++) {
        let element = data[i];
        var children = new _Row(this.tableParent, this.titleParent, i);
        this.$el.append(children.html);
        this.childrens.push(children);
        children.set(element);
    };
    this.renderWidth();
};
_Body.prototype.renderWidth = function (key) {
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        element.renderWidth(key);
    };
};
_Body.prototype.setCheckBox = function (type) {
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        element.setCheckBox(type);
    };
};
_Body.prototype.getCheckBoxValues = function (id) {
    var values = [];
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        values.push(element.getCheckBoxValue(id));
    };
    return values;
};
_Body.prototype.getCheckBoxRowValue = function (id) {
    var datas = [];
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        var value = element.getCheckBoxValue(id);
        if (value) {
            datas.push(element.data);
        };
    };
    return datas
};
_Body.prototype.getRadioRowValue = function (id) {
    var data = null;
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        data = element.getRadioValue(id);
        if (data) {
            break;
        };
    };
    return data
};
_Body.prototype.clearRadioValue = function (id) {
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        element.clearRadioValue(id);
    };
};
_Body.prototype.getShowValues = function () {
    var datas = [];
    for (let i = 0; i < this.childrens.length; i++) {
        const element = this.childrens[i];
        datas.push(element.getShowValues());
    };
    return datas;
};

var _Row = function (tableParent, titleParent, rowIndex) {
    this.cols = tableParent.cols;
    this.tableParent = tableParent;
    this.titleParent = titleParent;
    this.rowIndex = rowIndex;
    this.data = {};
    this.units = [];
    this.unitsObj = {};
    this._class = tableParent._class;
    this._theme = tableParent._theme;
    this.html = $(`<tr class="${this._class.row} ${this._theme.row}"></tr>`);
    this.html.click(() => {
        this.tableParent.events.rowClick(this.data, this.tableParent, this);
    });
    this.html.dblclick(() => {
        this.tableParent.events.rowDblclick(this.data, this.tableParent, this);
    });
};
_Row.prototype.set = function (data) {
    this.html.empty();
    this.data = data;
    this.units = [];
    for (let i = 0; i < this.titleParent.units.length; i++) {
        let titleElement = this.titleParent.units[i];
        var item = new _Unit(this.tableParent, titleElement, this);
        this.unitsObj[titleElement.options.key] = item;
        this.units.push(item);
        this.html.append(item.html);
        item.set(this.data);
    };
};
_Row.prototype.renderWidth = function (key) {
    if (key) {
        this.unitsObj[key].renderWidth();
    } else {
        for (let i = 0; i < this.units.length; i++) {
            this.units[i].renderWidth();
        };
    };
};
_Row.prototype.get = function () {
    return this.data;
};
_Row.prototype.setCheckBox = function (type) {
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.hasOwnProperty('checkBox')) {
            const item = element['checkBox'];
            item.set(type);
        };
    }
};
_Row.prototype.getCheckBoxValue = function (id) {
    var value = false;
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.id == id) {
            value = element.checkBox.value;
            break;
        };
    };
    return value;
};
_Row.prototype.getRadioValue = function (id) {
    var data = null;
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.id == id && element.radio.value) {
            data = this.data;
            break;
        };
    };
    return data;
};
_Row.prototype.clearRadioValue = function (id) {
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.id == id) {
            element.radio.unSelect();
            break;
        };
    };
};
_Row.prototype.getShowValues = function () {
    var datas = {};
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        const key = element.titleParent.options.key;
        if (key) {
            datas[key] = element.data;
        };
    };
    return datas;
};
_Row.prototype.radioSelect = function () {
    for (let i = 0; i < this.units.length; i++) {
        const element = this.units[i];
        if (element.hasOwnProperty('radio')) {
            element.radioSelect();
        };
    };
};


var _Unit = function (tableParent, titleParent, rowParent) {
    this.id = titleParent.id;
    this.tableParent = tableParent;
    this.titleParent = titleParent;
    this.rowParent = rowParent;
    this._class = tableParent._class;
    this._theme = tableParent._theme;
    this.html = $(`<td></td>`);
    this.$el = $(`<div class="${this._class.unit} ${this._theme.unit}">&nbsp;</div>`).appendTo(this.html);
    this.rowData = {};
    this.data = null;
    if (this.titleParent.options.type == 'checkBox' || this.titleParent.options.type == 'radio') {
        this.$el.css('min-width', '40px');
    };

    this.$el.css({
        'text-align': this.titleParent.options.textAlign,
    });

    if (this.titleParent.options.type == 'checkBox') {
        this.checkBox = new checkBoxItem({
            data: {
                name: ''
            },
            onChange: value => {
                this.titleParent.checkBoxValue(value);
            },
        });
        this.$el.empty();
        this.$el.append(this.checkBox.html);
    } else if (this.titleParent.options.type == 'radio') {
        this.radio = new radioItem((event) => {
            this.radioSelect();
        });
        this.$el.empty();
        this.$el.append(this.radio.html);
    };
};
_Unit.prototype.radioSelect = function () {
    this.tableParent.objs.body.clearRadioValue(this.id);
    this.radio.select();
};
_Unit.prototype.set = function (data) {
    this.rowData = data;
    var type = this.titleParent.options.type;
    var key = this.titleParent.options.key;
    this.data = data[key];

    if (this.titleParent.options.type == 'index') {
        var _value = 0;
        if (this.tableParent.options.followPage && this.tableParent.parentEvent && this.tableParent.parentEvent.pagingObject && this.tableParent.parentEvent.pagingObject.options.data.pageInfo) {
            var pageOptions = this.tableParent.parentEvent.pagingObject.options.data.pageInfo;
            _value = (parseInt(pageOptions.index) - 1) * parseInt(pageOptions.count);
        };
        this.data = this.rowParent.rowIndex + _value + 1;
    };

    if (this.titleParent.typeFormat) {
        switch (this.titleParent.typeFormat.key) {
            case 'OptionSide':
                var value = this.titleParent.typeFormat.value.split(':');
                if (value.length == 1) {
                    var OptionSideKeyType = value[0];
                    var OptionSideKey = 'value';
                    var OptionSideValue = 'label';
                } else {
                    var OptionSideKeyType = value[0];
                    var OptionSideKey = value[1];
                    var OptionSideValue = value[2];
                };
                var OptionSideData = optionSide.get(OptionSideKeyType, 'object', OptionSideKey);
                var OptionSideDataItem = OptionSideData[this.data];
                this.data = OptionSideDataItem ? OptionSideDataItem[OptionSideValue] : this.data;
                break;
            default:
                break;
        };
    };
    if (this.titleParent.options.hasOwnProperty('format') && this.titleParent.options.format) {
        var format = this.titleParent.options.format;
        var ans = format(this.rowData, this.data, this.$el, {
            unit: this,
            row: this.rowParent,
            title: this.titleParent,
            table: this.tableParent,
        });
        if (ans !== undefined) {
            this.data = ans;
        };
    };
    if (this.titleParent.options.type == 'buttons') {
        if (this.titleParent.options.hasOwnProperty('buttons') && this.titleParent.options.buttons.length > 0) {
            var buttons = this.titleParent.options.buttons;
            for (let i = 0; i < buttons.length; i++) {
                const element = buttons[i];
                this.$el.append(element(this.html, this.rowParent.html, this.rowData, {
                    unit: this,
                    row: this.rowParent,
                    title: this.titleParent,
                    table: this.tableParent,
                }));
            };
        };
    } else {
        this.$el.html(this.data);
        this.$el.attr("title", this.data)
    };
};
_Unit.prototype.renderWidth = function () {
    var width = this.titleParent.styleWidth;
    if (this.titleParent.options.isLast) {
        width += 1;
    };
    this.$el.css('width', width + 'px');
};

var _Screen = function () {
    this.Keys = {};
    this.cacheKeyArray = [];
};
_Screen.prototype.setCols = function (cols) {
    this.Keys = {};
    for (let i = 0; i < cols.length; i++) {
        const item = cols[i];
        if (item.key) {
            this.cacheKeyArray.push(item.key);
            this.Keys[item.key] = item;
        } else {
            this.Keys[util.uuid()] = item;
        };
    };
    return util.clone(cols);
};
_Screen.prototype.getKeyValues = function () {
    var data = [];
    for (const key in this.Keys) {
        if (this.Keys.hasOwnProperty(key)) {
            const element = this.Keys[key];
            if (element.type != 'checkBox' && element.type != 'radio' && element.type != 'buttons') {
                data.push({
                    label: element.name,
                    value: element.key
                });
            };
        }
    };
    return data;
};
_Screen.prototype.getShowKeyValue = function () {
    return this.cacheKeyArray;
};
_Screen.prototype.ChangeCols = function (keyArray) {
    this.cacheKeyArray = keyArray;
    var _cols = [];
    for (const key in this.Keys) {
        if (this.Keys.hasOwnProperty(key)) {
            const element = this.Keys[key];
            if (element.type != 'checkBox' && element.type != 'radio' && element.type != 'buttons') {
                if (keyArray.indexOf(element.key) != -1) {
                    _cols.push(element);
                }
            } else {
                _cols.push(element);
            };
        }
    };
    return util.clone(_cols);
};

var Table = function (options, parentObject) {
    this.options = $.extend({}, {
        title: '表格数据',
        class: 'FD-UI',
        theme: 'default',
        minWidth: 120,
        drag: false,
        move: false,
        screen: false,
        sort: 'local',  // sync 或 local
        height: false,
        titleAlign: 'left',
        followPage: false,
    }, options);
    this._class = this.classMappingEvent(this.options.class);
    this._theme = this.classMappingEvent(this.options.class + '-' + this.options.theme);

    this.parentEvent = parentObject;

    this.cols = [];

    this.html = $(`<div class="${this._class.box} ${this._theme.box}"><div class="scroll-mark"></div></div>`);
    this.events = {
        rowClick: function () { },
        rowDblclick: function () { },
    };
    this.data = null;
    this.oldData = null;
    this.objs = {};
    this.objs.title = new _Title(this);
    this.objs.body = new _Body(this);
    this.objs.screen = new _Screen(this);

    this.html.append(this.objs.title.html);
    this.html.append(this.objs.body.html);

    // $(window).resize(() => {
    //     this.objs.title.setWidth();
    // });

};
Table.prototype.sortBy = function (key, type) {
    if (this.options.sort == 'local') {
        if (this.oldData === null) {
            this.oldData = util.clone(this.data);
        };
        if (this.data && this.data.length > 0 && this.data[0].hasOwnProperty(key)) {
            if (type) {
                if (type == 'asc') {
                    var Event = function (a, b) {
                        if (isNumber(a[key]) && isNumber(b[key])) {
                            return a[key] - b[key];
                        } else {
                            return a[key].localeCompare(b[key]);
                        }
                    }
                } else if (type == 'gra') {
                    var Event = function (a, b) {
                        if (isNumber(a[key]) && isNumber(b[key])) {
                            return -a[key] - b[key];
                        } else {
                            return -a[key].localeCompare(b[key]);
                        }
                    }
                };
                var data = this.data.sort(Event);
                this.objs.body.setData(data);
            } else {
                this.objs.body.setData(util.clone(this.oldData));
            };
        };
    } else if (this.options.sort == 'sync') {
        // 异步排序方法
    };
};
Table.prototype.render = function () {
    return this.html;
};
Table.prototype.setCols = function (cols) {
    this.cols = this.objs.screen.setCols(cols);
    this.objs.title.render();
    if (this.data) {
        this.objs.body.setData(this.data);
    };
};
Table.prototype.ChangeCols = function (keyArray) {
    this.cols = this.objs.screen.ChangeCols(keyArray);
    this.objs.title.render();
    if (this.data) {
        this.objs.body.setData(this.data);
    };
};
Table.prototype.set = function (data) {
    this.data = data;
    this.objs.body.setData(data);
    this.objs.title.setWidth();
};
Table.prototype.on = function (str, event) {
    if (this.events.hasOwnProperty(str)) {
        this.events[str] = event;
    };
    // var keyArray = str.split('.');
    // var _obj = this.events;
    // var isComplate = true;
    // for (let i = 0; i < keyArray.length; i++) {
    //     const element = keyArray[i];
    //     if (this.events.hasOwnProperty(element)) {
    //         _obj = this.events[element];
    //     } else {
    //         isComplate = false;
    //     }
    // };
    // if (isComplate && typeof _obj == 'function') {
    //     _obj = event;
    // };
};
Table.prototype.getCheckBoxValue = function () {
    var id = this.objs.title.getCheckBoxId();
    if (id) {
        var datas = this.objs.body.getCheckBoxRowValue(id);
        return datas;
    } else {
        console.error('Table => 未找到 checkBox 选项');
        return [];
    };
};
Table.prototype.getRadioValue = function () {
    var id = this.objs.title.getRadioId();
    if (id) {
        var data = this.objs.body.getRadioRowValue(id);
        return data;
    } else {
        console.error('Table => 未找到 Radio 选项');
        return [];
    };
};



// 生成表格样式集合
Table.prototype.classMappingEvent = function (str) {
    return {
        box: str + '-table-box',
        table: str + '-table',
        header: str + '-header',
        headerBox: str + '-header-box',
        headerUnit: str + '-header-unit',
        headerDrag: str + '-header-drag',
        sort: {
            default: str + '-sort-default',
            up: str + '-sort-up',
            down: str + '-sort-down',
        },
        body: str + '-body',
        bodyBox: str + '-body-box',
        row: str + '-row',
        unit: str + '-unit',
        freeze: {
            left: str + '-freeze-left',
            right: str + '-freeze-right',
            header: str + '-freeze-header',
            box: str + '-freeze-box',
        },
    };
};
Table.prototype.exportFile = function (type) {
    type = type || 'csv';
    var exportType = fileType[type];
    var alink = document.createElement("a");
    alink.style.display = 'none';
    var device = util.device();

    if (device.ie) {
        return;
    };

    var formatStr = () => {
        var dataTitle = this.objs.title.getTitleValues();
        var dataMain = this.objs.body.getShowValues();
        var titleArray = [];
        for (let i = 0; i < dataTitle.length; i++) {
            const element = dataTitle[i];
            titleArray.push('"' + element.name + '"');
        };
        var bodyArray = [];
        for (let i = 0; i < dataMain.length; i++) {
            const cols = dataMain[i];
            var colsValue = [];
            for (let d = 0; d < dataTitle.length; d++) {
                const title = dataTitle[d];
                colsValue.push('"\t' + cols[title.key] + '"');
            };
            bodyArray.push(colsValue.join(','));
        };
        return titleArray.join(',') + '\r\n' + bodyArray.join('\r\n') + '\r\n';
    }
    alink.href = 'data:' + exportType + ';charset=utf-8,\ufeff' + encodeURIComponent(formatStr());
    alink.download = this.options.title + '.' + type;
    document.body.appendChild(alink);
    alink.click();
    document.body.removeChild(alink);
};
Table.prototype.print = function () {
    var printWin = window.open('打印窗口', '_blank');
    var style = ['<style>',
        'body{font-size: 12px; color: #666;}',
        'table{width: 100%; border-collapse: collapse; border-spacing: 0;}',
        'th,td{line-height: 20px; padding: 9px 15px; border: 1px solid #ccc; text-align: left; font-size: 12px; color: #666;}',
        'a{color: #666; text-decoration:none;}',
        '*.layui-hide{display: none}',
        '</style>'].join('');

    var dataTitle = this.objs.title.getTitleValues();
    var dataMain = this.objs.body.getShowValues();

    var titleArray = [];
    for (let i = 0; i < dataTitle.length; i++) {
        const element = dataTitle[i];
        titleArray.push('<th>' + element.name + '</th>');
    };
    var bodyArray = [];
    for (let i = 0; i < dataMain.length; i++) {
        const cols = dataMain[i];
        var colsValue = [];
        for (let d = 0; d < dataTitle.length; d++) {
            const title = dataTitle[d];
            colsValue.push('<td>' + cols[title.key] + '</td>');
        };
        var colString = colsValue.join('');
        bodyArray.push(colString);
    };
    var html = '<table><thead><tr>';
    html += titleArray.join('');
    html += '<tr></thead><tbody>';
    html += bodyArray.join('</tr><tr>');
    html += '</tbody></table>';
    printWin.document.write(style + html);
    printWin.document.close();
    printWin.print();
    printWin.close();
};



export default Table;
